package com.zoweunion.mechanic.util;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.plugins.websocket.WebSocketServer;
import com.zoweunion.mechanic.service.base.MessageRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class WebSocketPushUtil {

	@Autowired
	private WebSocketServer webSocketServer;

	@Autowired
	private MessageRecordService messageRecordService;

	public void websocketPush(Map<String, Object> map, String sendUserId){
		webSocketServer.sendtoUser(JSONObject.toJSONString(map),sendUserId);
		try {
			map.put("user",map.get("current_user_id").toString());
			map.put("extra_params",map.get("extra_params").toString());
			map.put("orderNo",map.get("order_id").toString());
			map.put("receive_user",sendUserId);
			map.put("context",map.get("msg").toString());
			map.put("send_type", map.get("type") == null ? 0 : Integer.valueOf(map.get("type").toString()));
			map.put("client","PC");
			messageRecordService.addMessageRecord(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
